<template>
  <div class="food-story-page">
    <el-card
      class="aui-card--fill"
      shadow="never">
      <el-form inline>
        <!--        <el-form-item>-->
        <!--          <el-input placeholder="故事名"></el-input>-->
        <!--        </el-form-item>-->
        <!--        <el-form-item prop="cityId">-->
        <!--          <el-select v-model="dataForm.cityId" placeholder="城市" clearable>-->
        <!--            <el-option-->
        <!--              v-for="city in cities"-->
        <!--              :key="'city-' + city.id"-->
        <!--              :value="city.id"-->
        <!--              :label="city.name"-->
        <!--            ></el-option>-->
        <!--          </el-select>-->
        <!--        </el-form-item>-->
        <el-form-item>
          <el-button
            @click="clickOnAddOrUpdate()"
            type="primary">
            <i class="el-icon-plus"/>
            <span>新增故事</span>
          </el-button>
        </el-form-item>
      </el-form>

      <div class="table-container">
        <el-table
          :data="dataList"
          border
          style="width: 100%;"
          v-loading="dataListLoading"
        >
          <!--                    <el-table-column label="ID" prop="id"></el-table-column>-->

          <el-table-column
            label="故事主图"
            prop="cityUrl"
            width="80">
            <template slot-scope="scope">
              <midea-admin-image
                :height="60"
                :previewable="true"
                :url="scope.row.cityUrl"
                :width="60"
              />
            </template>
          </el-table-column>
          <el-table-column
            label="故事标题"
            prop="title"/>
          <el-table-column
            label="故事描述"
            prop="descri"
            show-overflow-tooltip
          />

          <el-table-column
            label="城市"
            prop="cityId">
            <template slot-scope="scope">
              {{ getStoryCityName(scope.row.cityId) }}
            </template>
          </el-table-column>
          <!--          <el-table-column label="城市" prop="cityUrl"></el-table-column>-->
          <el-table-column
            :label="$t('handle')"
            align="center"
            fixed="right"
            header-align="center"
            width="160"
          >
            <template slot-scope="scope">
              <el-button
                @click="clickOnAddOrUpdate(scope.row.id)"
                size="small"
                type="primary"
                plain
              >
                {{ $t("update") }}
              </el-button>
              <el-button
                @click="deleteHandle(scope.row.id)"
                size="small"
                plain
                type="danger"
              >{{ $t("delete") }}
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          :current-page="page"
          :page-size="limit"
          :page-sizes="[10, 20, 50, 100]"
          :total="total"
          @current-change="pageCurrentChangeHandle"
          @size-change="pageSizeChangeHandle"
          layout="total, sizes, prev, pager, next, jumper"
        />
      </div>
      <!-- 弹窗, 新增 / 修改 -->

      <citystory-add-or-update
        @refreshDataList="getDataList"
        ref="addOrUpdate"
        v-if="addOrUpdateVisible"
      />
    </el-card>
  </div>
</template>

<script>
import mixinViewModule from '@/mixins/mike.view-module'
import CitystoryAddOrUpdate from './citystory-add-or-update'
import cloneDeep from 'lodash/cloneDeep'
export default {
  name: 'CityStory',
  components: { CitystoryAddOrUpdate },
  mixins: [mixinViewModule],
  data () {
    return {
      cities: [],
      mixinViewModuleOptions: {
        getDataListURL: '/midea/menu/foodmap/citystorys/getall',
        getDataListIsPage: true,
        deleteURL: '/midea/menu/foodmap/citystorys/del/'
        // deleteIsBatch: true
      },
      dataForm: {
        // cityId: ''
      }
    }
  },
  mounted () {
    this.getDataList()
    this.getCities()
  },
  methods: {
    getStoryCityName (cityId) {
      return this.cities.find(item => {
        return item.id === cityId
      }).name
    },
    getCities () {
      this.$http
        .post(`${this.mikeNetworkBase}midea/menu/foodmap/common/citys/`)
        .then(({ data: res }) => {
          if (res.code !== 200) {
            return this.$message.error(res.msg)
          }

          this.cities = res.data
        })
    },

    clickOnAddOrUpdate (id) {
      this.addOrUpdateVisible = true
      let model = this.dataList.find(item => {
        return item.id === id
      })
      this.$nextTick(() => {
        this.$refs.addOrUpdate.reset()
        if (model) {
          // model
          this.$refs.addOrUpdate.form = cloneDeep(model)
        }

        this.$refs.addOrUpdate.init()
      })
    }
  }
}
</script>

<style scoped lang="scss"></style>
